package com.example.myapplication;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;

public class GuideActivity extends AppCompatActivity {
    ViewPager vp;
    LinearLayout layout;
    ArrayList<ImageView> list;
    int resId[] = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d};
    GuideAdapter adapter;
    ArrayList<ImageView> pointlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        vp = findViewById(R.id.guide_vp);
        layout = findViewById(R.id.guide_layout);

        list = new ArrayList<>();
        pointlist = new ArrayList<>();
        initImage();
        adapter = new GuideAdapter(this, list);
        vp.setAdapter(adapter);

        vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override

            public void onPageScrollStateChanged(int posittion) {
                for (int i = 0; i < pointlist.size(); i++) {
                    pointlist.get(i).setImageResource(R.mipmap.p0);
                }
                pointlist.get(posittion%pointlist.size()).setImageResource(R.mipmap.p1);

            }
        });
        vp.setCurrentItem(100);
    }
    private void initImage() {
        for (int i = 0; i < resId.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(resId[i]);
            list.add(imageView);

            ImageView piv = new ImageView(this);
            piv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            piv.setImageResource(R.mipmap.p0);
            pointlist.add(piv);
            layout.addView(piv);
        }
        pointlist.get(0).setImageResource(R.mipmap.p1);
    }
}
